<?php

function kalkulasi_komponen($komponen_gaji_id , $karyawan_id){	
	$hasil_type_entry = 0;
	$komponen = my_get_data_by_id( 'pr_komponen_gaji' , 'komponen_gaji_id' , $komponen_gaji_id );
	$periode_id =get_periode_gaji();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$tanggal = $periode['tanggal'];		
	
	$query = "SELECT * FROM pr_penyesuaian
		WHERE karyawan_id={$karyawan_id} 
		AND komponen_id={$komponen_gaji_id}" ;
	$result = my_query($query); 
	$exceptions = my_fetch_array($result);
	if(my_num_rows($result) > 0){	
		$hasil_type_entry = $exceptions['penyesuaian'] ;
	}elseif($komponen['model_entry'] == '1'){ //upload
		$query = "
			SELECT nominal FROM pr_gaji_upload_view 
			WHERE karyawan_id = {$karyawan_id} 
			AND komponen_gaji_id={$komponen_gaji_id}
		";	 
		$dres = my_query($query);
		$drow = my_fetch_array($dres);
		$hasil_type_entry = $drow['nominal'];	
	}elseif($komponen['model_entry'] == '2'){ //rumus
		$trr=formula_normatif($komponen['formula_normatif'], $karyawan_id);  
		if((int)strlen(trim($trr)) <> 0){			
			eval("\$hasil_type_entry  =  ".  $trr .";" ) ;  
		}	
		if( (int) $komponen['komponen_gaji_id'] == 1 ){
			$karyawan = loaddata_karyawan($karyawan_id);
			if($karyawan['ptkp_status'] == 'TK') $hasil_type_entry  = 0;
		}elseif((int) $komponen['komponen_gaji_id'] == 2){
			$karyawan = loaddata_karyawan($karyawan_id);
			if($karyawan['ptkp_status'] == 'K') $hasil_type_entry  = 0;
		}
	}elseif($komponen['model_entry'] == '3'){  //jabatan
		$sk_id=get_sk_id($tanggal,$komponen_gaji_id);
		$query = "
			SELECT nominal FROM pr_sk_tunjangan_detail a
			INNER JOIN kekaryawanan b ON b.golongan_jabatan_id = a.sub_id 
			WHERE b.karyawan_id = {$karyawan_id } 
			AND a.komponen_gaji_id={$komponen_gaji_id} 
			AND a.sk_id={$sk_id}
		";	
		$dres = my_query($query);
		$drow = my_fetch_array($dres);
		$hasil_type_entry = $drow['nominal'];
	}elseif($komponen['model_entry'] == '4'){  //profesi
		$sk_id=get_sk_id($tanggal,$komponen_gaji_id);
		$query = "
			SELECT nominal FROM pr_sk_tunjangan_detail a
			INNER JOIN kekaryawanan b ON b.profesi_detail_id = a.sub_id 
			WHERE b.karyawan_id = {$karyawan_id } 
			AND a.komponen_gaji_id={$komponen_gaji_id} 
			AND a.sk_id={$sk_id}
		";	 
		$dres = my_query($query);
		$drow = my_fetch_array($dres);
		$hasil_type_entry = $drow['nominal'];
	}elseif($komponen['model_entry'] == '5'){  //nilai pajak
		$hasil_type_entry = 0;		
	}elseif($komponen['model_entry'] == '6'){  //potongan limit waktu
		$query = "
			SELECT potongan FROM pr_potongan_limit
			WHERE komponen_gaji_id = {$komponen_gaji_id} 
			AND aktif = 'Y' 
			AND karyawan_id = {$karyawan_id } 
			AND '".$tanggal."' 
			BETWEEN tanggal_mulai AND tanggal_selesai
		";	
		$dres = my_query($query);
		$drow = my_fetch_array($dres);
		$hasil_type_entry = $drow['potongan'];	
	}elseif($komponen['model_entry'] == '7'){  //selisih pembulatan
		$hasil_type_entry = 0;				
	}
	return round($hasil_type_entry,0);
}

function formula_normatif( $str , $karyawan_id){
	$periode_id =get_periode_gaji();

	$formulas = array();
	$formulas['[GAPOK]'] = 'formula_get_gapok($karyawan_id)';
	$formulas['[MASA_KERJA_TAHUN]'] = 'formula_get_masa_kerja($karyawan_id)';
	$formulas['[TANGGUNGAN_KELUARGA]'] = 'formula_get_marital_status($karyawan_id)';
	$formulas['[HUT_TMB]'] = 'formula_get_hut_tmb($karyawan_id)';

	$text = $str;
	foreach($formulas as $key => $val ){ 
		$text  = str_replace($key , $val , $text); 
	}
	return $text ;
}

function formula_get_gapok($karyawan_id){
 
	$query  = "SELECT gapok_km FROM kekaryawanan WHERE karyawan_id = {$karyawan_id} ";
	$res = my_query($query);
	$rw = my_fetch_array($res);
	
	return $rw['gapok_km'] ;
}	

function formula_get_masa_kerja($karyawan_id){
	$query = "SELECT tmb FROM kekaryawanan WHERE karyawan_id={$karyawan_id}";
	$res = my_query($query);
	$rw = my_fetch_array($res);	
	return getage($tr['tmb']);
}	

function formula_get_marital_status($karyawan_id){
 
	$query  = "SELECT marital FROM kekaryawanan WHERE karyawan_id = {$karyawan_id} ";
	$res = my_query($query);
	$rw = my_fetch_array($res);
	
	if($rw['marital'] =='K')return 1;
	
	return 0;
}	


function formula_get_hut_tmb($karyawan_id){
	//nilai return 4=cuti besar, 1=cuti tahunan, 0=else
	$periode_id =get_periode_gaji();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = (int) date('Y' , strtotime($periode['tanggal']));
	
	$query  = "SELECT tmb FROM kekaryawanan WHERE karyawan_id = {$karyawan_id} ";
	$rw = my_fetch_array(my_query($query));
	$tmb=$rw['tmb'] ;
	$bulan_tmb = (int) date('m' , strtotime($tmb));
	$tahun_tmb = (int) date('Y' , strtotime($tmb));
	if($bulan==$bulan_tmb){
		if($tahun==$tahun_tmb){
			return 1;
		}
		$masatahun= $tahun-$tahun_tmb;
		if($tahun_tmb < 2000){
			$temp= (2000-$tahun_tmb)%3 ;
			if($temp==0){
				$temp=3;
			}
			$temp=($tahun-(2000-$temp))/4;
			$temp= $temp  - (int) $temp ;
			if($temp==0){
				return 4;
			}
			return 1;
		}else{
			$temp=($masatahun/4);
			$temp=$temp - (int) $temp ;
			if($temp==0){
				return 4;
			}   
			return 1;
		}
	}	
	return 0;
}	

function get_sk_id($tanggal,$komponen_gaji_id){	
	$query = "
		SELECT sk_id FROM pr_sk_tunjangan 
		WHERE komponen_gaji_id =".$komponen_gaji_id." 
		AND tanggal<='".$tanggal."' 
		ORDER BY tanggal DESC LIMIT 1
		";
	$rw = my_fetch_array(my_query($query));
	$sk_id= isset( $rw['sk_id'] ) ? $rw['sk_id']:  0;
	return $sk_id ;
	
}	